package net.roobook.common.handler;

import com.alibaba.fastjson.JSONObject;
import net.roobook.common.entity.RoobookResponse;
import net.roobook.common.utils.RoobookUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Roobook
 * @description 资源服务器异常，处理令牌不正确返回401
 * @date 2022/4/8
 */
public class RoobookAuthExceptionEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        RoobookResponse roobookResponse = new RoobookResponse();
        RoobookUtil.makeResponse(
                // application/json;charset=UTF-8
                response, MediaType.APPLICATION_JSON_UTF8_VALUE,
                // 401
                HttpServletResponse.SC_UNAUTHORIZED, roobookResponse.message("token无效")
        );
    }
}
